<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="综艺id" prop="varietyId">
        <el-select v-model="queryParams.varietyId" clearable filterable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.varietyId"
            :label="item.varietyName"
            :value="item.varietyId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="综艺视频解锁设置" prop="varietyVideoUnlock">

        <el-select v-model="queryParams.varietyVideoUnlock" placeholder="请选择" clearable size="small">
          <el-option label="请选择" value="" />
          <el-option label="免费" value="0" />
          <el-option label="金币付费" value="1" />
          <el-option label="会员免费" value="2" />

        </el-select>
      </el-form-item>
      <el-form-item label="时间" prop="varietyVideoTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.varietyVideoTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否上架" prop="varietyVideoPutaway">
        <el-select v-model="queryParams.varietyVideoPutaway" placeholder="请选择" clearable size="small">
          <el-option label="请选择" value="" />
          <el-option label="已上架" value="0" />
          <el-option label="未上架" value="1" />


        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['xqsp:varietyVideo:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['xqsp:varietyVideo:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['xqsp:varietyVideo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xqsp:varietyVideo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="varietyVideoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="综艺视频编号" align="center" prop="spVarietyVideo.varietyVideoId" />
      <el-table-column label="综艺" align="center" prop="varietyName" />
      <el-table-column label="综艺视频名字" align="center" prop="spVarietyVideo.varietyVideoName" />
      <el-table-column label="综艺视频封面" align="center" prop="spVarietyVideo.varietyVideoCoverImg">
        <template slot-scope="scope">
          <el-image :src="scope.row.spVarietyVideo.varietyVideoCoverImg"  :preview-src-list="[scope.row.spVarietyVideo.varietyVideoCoverImg]" style="width: 100px;height: 100px"/>
        </template>
      </el-table-column>

      <el-table-column label="清晰度设置" align="center" prop="spVarietyVideo.varietyVideoDefinition" />
      <el-table-column label="综艺视频地址" align="center" prop="spVarietyVideo.varietyVideoUrl" />
      <el-table-column label="视频预览" align="center" prop="spVarietyVideo.varietyVideoUrlPreview" />
      <el-table-column label="综艺视频观看次数" align="center" prop="spVarietyVideo.varietyVideoWatch" />
      <el-table-column label="综艺视频点赞次数" align="center" prop="spVarietyVideo.varietyVideoLikeNumber" />
      <el-table-column label="综艺视频差评次数" align="center" prop="spVarietyVideo.varietyVideoUnlikeNumber" />
      <el-table-column label="收藏人数" align="center" prop="spVarietyVideo.varietyVideoCollectNumber" />
      <el-table-column label="综艺视频解锁设置" align="center" prop="spVarietyVideo.varietyVideoUnlock" />
      <el-table-column label="解锁视频所需金币数" align="center" prop="spVarietyVideo.varietyVideoUnlockGold" />
      <el-table-column label="时间" align="center" prop="spVarietyVideo.varietyVideoTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.spVarietyVideo.varietyVideoTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="视频时长" align="center" prop="spVarietyVideo.varietyVideoDuration" />
      <el-table-column label="是否上架" align="center" prop="spVarietyVideo.varietyVideoPutaway">

        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.spVarietyVideo.varietyVideoPutaway"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row)">

          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xqsp:varietyVideo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xqsp:varietyVideo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改综艺视频对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="综艺视频名字" prop="varietyVideoName">
          <el-input v-model="form.varietyVideoName" placeholder="请输入综艺视频名字" />
        </el-form-item>
        <el-form-item label="综艺视频封面" prop="varietyVideoCoverImg">
          <el-input v-model="form.varietyVideoCoverImg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="综艺id:" prop="varietyId">

          <el-select v-model="form.varietyId" clearable filterable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.varietyId"
              :label="item.varietyName"
              :value="item.varietyId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="综艺视频地址:" prop="varietyVideoUrl">
          <el-input v-model="form.varietyVideoUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="视频预览" prop="varietyVideoUrlPreview">
          <el-input v-model="form.varietyVideoUrlPreview" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="清晰度设置" prop="varietyVideoDefinition">
          <el-select v-model="form.varietyVideoDefinition" clearable filterable placeholder="请选择清晰度">
            <el-option label="请选择清晰度" value="" />
            <el-option label="标清" value="0" />
            <el-option label="高清" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="综艺视频观看次数:" prop="varietyVideoWatch">
          <el-input v-model="form.varietyVideoWatch" placeholder="请输入综艺视频观看次数(默认为0)" />
        </el-form-item>
        <el-form-item label="综艺视频点赞次数:" prop="varietyVideoLikeNumber">
          <el-input v-model="form.varietyVideoLikeNumber" placeholder="请输入综艺视频点赞次数(默认为0)" />
        </el-form-item>
        <el-form-item label="综艺视频差评次数" prop="varietyVideoUnlikeNumber">
          <el-input v-model="form.varietyVideoUnlikeNumber" placeholder="请输入综艺视频差评次数(默认为0)" />
        </el-form-item>
        <el-form-item label="收藏人数" prop="varietyVideoCollectNumber">
          <el-input v-model="form.varietyVideoCollectNumber" placeholder="请输入收藏人数(默认为0)" />
        </el-form-item>
        <el-form-item label="综艺视频解锁设置:" prop="varietyVideoUnlock">
          <el-radio-group v-model="form.varietyVideoUnlock">

            <el-radio :label="0" >免费</el-radio>
            <el-radio :label="1"  >金币付费</el-radio>
            <el-radio :label="2" >会员免费</el-radio>

          </el-radio-group>
        </el-form-item>
        <el-form-item label="解锁视频所需金币数" prop="varietyVideoUnlockGold">
          <el-input v-model="form.varietyVideoUnlockGold" placeholder="请输入解锁视频所需金币数(默认为1)" />
        </el-form-item>
        <el-form-item label="视频时长" prop="varietyVideoDuration">
          <el-input v-model="form.varietyVideoDuration" placeholder="请输入视频时长" />
        </el-form-item>
<!--        <el-form-item label="时间:" prop="varietyVideoTime">-->
<!--          <el-date-picker clearable size="small"-->
<!--            v-model="form.varietyVideoTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd HH:mm:ss"-->
<!--            placeholder="选择时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVarietyVideo, getVarietyVideo, delVarietyVideo, addVarietyVideo, updateVarietyVideo, exportVarietyVideo ,changeUserStatus} from "@/api/xqsp/varietyVideo";
import { listVariety} from "@/api/xqsp/variety";
export default {
  name: "VarietyVideo",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 综艺视频表格数据
      varietyVideoList: [],
      //综艺下拉框
      options:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        varietyId: null,
        varietyVideoUrl: null,
        varietyVideoWatch: null,
        varietyVideoLikeNumber: null,
        varietyVideoUnlock: null,
        varietyVideoTime: null,
        varietyVideoPutaway: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        varietyVideoName: [
          {required: true, message: "综艺视频名字不能为空", trigger: "blur"}
        ],
        varietyVideoCoverImg: [
          {required: true, message: "综艺视频封面不能为空", trigger: "blur"}
        ],
        varietyVideoUrl: [
          {required: true, message: "综艺视频地址不能为空", trigger: "blur"}
        ],
        varietyVideoDefinition: [
          {required: true, message: "清晰度设置不能为空", trigger: "blur"}
        ],
        varietyVideoUnlock: [
          {required: true, message: "综艺视频付费设置不能为空", trigger: "blur"}
        ],
        varietyVideoLikeNumber:[
          {
            pattern: /^[+]{0,1}(\d+)$/,
            message: '请输入正整数',
            trigger: 'blur'
          }
        ],
        varietyVideoWatch:[
          {
            pattern: /^[+]{0,1}(\d+)$/,
            message: '请输入正整数',
            trigger: 'blur'
          }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getVarietyList();
  },
  methods: {

    /** 查询综艺管理列表 */
    getVarietyList() {
      listVariety(this.queryParams).then(response => {
        this.options = response.rows;
      });
    },


    /** 查询综艺视频列表 */
    getList() {
      this.loading = true;
      listVarietyVideo(this.queryParams).then(response => {
        this.varietyVideoList = response.rows;
        this.varietyVideoList.forEach(item=>{
          if (item.spVarietyVideo.varietyVideoUnlock==0){
            item.spVarietyVideo.varietyVideoUnlock="免费"
          }else if (item.spVarietyVideo.varietyVideoUnlock==1){
            item.spVarietyVideo.varietyVideoUnlock="金币付费"
          }else if (item.spVarietyVideo.varietyVideoUnlock==2){
            item.spVarietyVideo.varietyVideoUnlock="会员免费"
          }
          if (item.spVarietyVideo.varietyVideoDefinition==0){
            item.spVarietyVideo.varietyVideoDefinition="标清"
          }else if (item.spVarietyVideo.varietyVideoDefinition==1){
            item.spVarietyVideo.varietyVideoDefinition="高清"
          }

        });




        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        varietyVideoId: null,
        varietyId: null,
        varietyVideoUrl: null,
        varietyVideoWatch: null,
        varietyVideoLikeNumber: null,
        varietyVideoUnlock: null,
        varietyVideoTime: null,
        varietyVideoPutaway: null,
        varietyVideoName:null,
        varietyVideoCoverImg:null,
        varietyVideoDefinition:null,
        varietyVideoUrlPreview:null,
        varietyVideoDuration: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.spVarietyVideo.varietyVideoId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加综艺视频";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const varietyVideoId = row.spVarietyVideo.varietyVideoId || this.ids
      getVarietyVideo(varietyVideoId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改综艺视频";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.varietyVideoId != null) {
            updateVarietyVideo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVarietyVideo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let tmpIds=[]
      if (this.ids.length>0){
        for (const id of this.ids) {
          tmpIds.push(id);
        }
      }else {
        tmpIds.push(row.spVarietyVideo.varietyVideoId);
      }
      this.$confirm('是否确认删除综艺视频编号为"' + tmpIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delVarietyVideo(tmpIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有综艺视频数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportVarietyVideo(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    // 是否上架
    handleStatusChange(row) {
      let text = row.spVarietyVideo.varietyVideoPutaway === 0 ? "上架" : "下架";
      this.$confirm('确认要"' + text + '""' + row.spVarietyVideo.varietyVideoId + '"视频吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return changeUserStatus(row.spVarietyVideo.varietyVideoId, row.spVarietyVideo.varietyVideoPutaway);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function() {
        row.spVarietyVideo.varietyVideoPutaway = row.spVarietyVideo.varietyVideoPutaway === 0 ? 1 : 0;
      });
    },
  }
};
</script>
